//
//  UIActionSheet+Block.m
//  CRM
//
//  Created by Wan Kiwi on 10/19/14.
//  Copyright (c) 2014 CC Inc. All rights reserved.
//

#import "UIActionSheet+Block.h"
#import <objc/runtime.h>

@implementation UIActionSheet (Block)

static char key;

- (void)showWithCompleteBlock:(ActionSheetCompleteBlock) block {
    // set key & delegate
    if (block) {
        objc_removeAssociatedObjects(self);
        objc_setAssociatedObject(self, &key, block, OBJC_ASSOCIATION_COPY);
        self.delegate = self;
    }
    // show
    [self showInView:[UIApplication sharedApplication].keyWindow];
}

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
    ActionSheetCompleteBlock block = objc_getAssociatedObject(self, &key);
    if (block) {
        block(buttonIndex);
    }
}

@end
